package cn.stylefeng.guns.modular.agv.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.agv.entity.AgvStorageTaskList;
import cn.stylefeng.guns.modular.agv.mapper.AgvStorageTaskListMapper;
import cn.stylefeng.guns.modular.agv.model.params.AgvStorageTaskListParam;
import cn.stylefeng.guns.modular.agv.model.result.AgvStorageTaskListResult;
import  cn.stylefeng.guns.modular.agv.service.AgvStorageTaskListService;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOutParam;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOutService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * agv拣选任务下发列表 服务实现类
 * </p>
 *
 * @author zhangzh
 * @since 2025-07-08
 */
@Service
public class AgvStorageTaskListServiceImpl extends ServiceImpl<AgvStorageTaskListMapper, AgvStorageTaskList> implements AgvStorageTaskListService {

    @Autowired
    private HxStorageOutService hxStorageOutService;

    @Override
    public void add(AgvStorageTaskListParam param){
        AgvStorageTaskList entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AgvStorageTaskListParam param){
        AgvStorageTaskList agvStorageTaskList = this.getById(param.getId());

        /**
         * 更改对应得拣货状态
         */
        if(ToolUtil.isNotEmpty(agvStorageTaskList) && ToolUtil.isNotEmpty(agvStorageTaskList.getStorageId())){
            HxStorageOutParam update = new HxStorageOutParam();
            update.setId(agvStorageTaskList.getStorageId().intValue());
            update.setStorageState("待拣货");
            update.setStorageUser("");
            update.setStorageUserName("");
            update.setAgvStorageTask("否");
            update.setStorageTime(new Date());
            this.hxStorageOutService.update(update);
        }

        this.removeById(getKey(param));
    }

    @Override
    public void update(AgvStorageTaskListParam param){
        AgvStorageTaskList oldEntity = getOldEntity(param);
        AgvStorageTaskList newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AgvStorageTaskListResult findBySpec(AgvStorageTaskListParam param){
        return null;
    }

    @Override
    public List<AgvStorageTaskListResult> findListBySpec(AgvStorageTaskListParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(AgvStorageTaskListParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(AgvStorageTaskListParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AgvStorageTaskList getOldEntity(AgvStorageTaskListParam param) {
        return this.getById(getKey(param));
    }

    private AgvStorageTaskList getEntity(AgvStorageTaskListParam param) {
        AgvStorageTaskList entity = new AgvStorageTaskList();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
